package org.hive.media
{
	import org.hive.managers.plugin.VideoSWFManager;
	import org.hive.managers.StateManager;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import org.hive.events.VideoSWFEvent;
	import flash.events.Event;
	import org.hive.layout.MovieClipElement;
	import flash.utils.Dictionary;

	public dynamic class VideoSWF extends MovieClipElement
	{
		private var __labels : Array;
		private var __LABELS_BY_NAME:Dictionary;
		private var __LABELS_BY_FRAME:Dictionary;
		
		public function VideoSWF()
		{
			super();
			__LABELS_BY_NAME	= new Dictionary( true );
			__LABELS_BY_FRAME	= new Dictionary( true );
			__labels = this.currentLabels;
			
			for (var i:Number = 0; i < __labels.length; i++) {
				var frame:VideoSWFFrameLabel = new VideoSWFFrameLabel(__labels[i]);
				frame.parentVideoSWF = this;

				__LABELS_BY_NAME[ frame.name ] = frame;
				__LABELS_BY_FRAME[ frame.frame ] = frame;
		    }
		
		    addEventListener( Event.ENTER_FRAME, handleEnterFrame );
		}
		
		
		private function mapLabels():void
		{
			__labels = this.currentLabels;
		}
		
		private function handleEnterFrame( e:Event ):void
		{
			if( getLabelByFrame( currentFrame ) != null && VideoSWFManager.currentVideo == this ){
				dispatchEvent( new VideoSWFEvent(VideoSWFEvent.ON_LABEL, name, getLabelByFrame( currentFrame ).name) );
				getLabelByFrame( currentFrame ).dispatchEvent( new VideoSWFEvent(VideoSWFEvent.ON_LABEL, name, getLabelByFrame( currentFrame ).name) );
			}
			
		}
		
		public var transitionContainer:MovieClip;
		public var transitionAnimation:MovieClip;
		public var transitionGraphic:Bitmap;
		public function initTransitionGraphic( defaultGraphic:BitmapData ):void
		{
			transitionGraphic = new Bitmap( defaultGraphic, "auto", true );
			transitionContainer.addChild( transitionGraphic );
		}
		
		
		public function changeState( s:String ):void
		{
			var event:VideoSWFEvent = new VideoSWFEvent( VideoSWFEvent.ON_LABEL, name, s );
			dispatchEvent( event );
		}
		

		public function getLabelByName( name:String ):VideoSWFFrameLabel
		{
			return __LABELS_BY_NAME[ name ];
		}
		
		public function getLabelByFrame( frame:Number ):VideoSWFFrameLabel
		{
			return __LABELS_BY_FRAME[ frame ];
		}
		

		
	}
}